/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smarttmt.idu.web.beans;

import com.smarttmt.idu.ejb.domain.Constants;
import com.smarttmt.idu.web.behavior.Preloadable;
import com.smarttmt.idu.web.persistence.controller.UsuacontJpaController;
import com.smarttmt.idu.web.utilities.FacesUtils;
import com.smarttmt.idu.web.utilities.SendEmail;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

/**
 *
 * @author Nicolás Rubén Caballero Ortega (helaman58@gmail.com)
 */
@Named(value = "forgotPasswordBean")
@SessionScoped
public class ForgotPassword implements Serializable, Preloadable {

    private String userNameOrEmail;
    private String keyToReset;
    @PersistenceUnit(unitName = "smarttmtiduappPU")
    private EntityManagerFactory emf;
    @Resource
    private UserTransaction utx;
    private UsuacontJpaController usuacontJpaController;

    /**
     * Creates a new instance of forgotPassword
     */
    public ForgotPassword() {
    }

    @PostConstruct
    public void postConstructorForgotPassword() {
        usuacontJpaController = new UsuacontJpaController(utx, emf);
    }

    public void resetData() {
        userNameOrEmail = null;
        keyToReset = null;
    }

    @Override
    public void preload(Map<String, Object> parameters) throws Exception {
        resetData();
    }

    public String forgotPasswordOperation() {
        sentEmailToUser();
        return "index";
    }

    public void sentEmail() throws Exception {
        String userEmail = usuacontJpaController.getUserEmailByNomb(keyToReset);
        SendEmail sendEmail = (SendEmail) FacesUtils.getManagedBean("sendEmail");
        sendEmail.sendMessage(keyToReset, Constants.FORGOT_FILENAME, Constants.FORGOT_TITLE, userEmail);
    }

    public boolean sentEmailToUser() {
        generateURLToResetPassword();
        try {
            sentEmail();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Un correo ha sido enviado a su dirección de correo electónico", "Verifique su bandeja de entrada..."));
            return true;
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "No hemos podido enviarle la información de restablecimiento de contraseña a su direccion de correo electronico", "Verificar la informacion provista..."));
            return false;
        }
    }

    public boolean generateURLToResetPassword() {
        keyToReset = userNameOrEmail;
        return true;
    }

    public String getUserNameOrEmail() {
        return userNameOrEmail;
    }

    public void setUserNameOrEmail(String userNameOrEmail) {
        this.userNameOrEmail = userNameOrEmail;
    }

    public String getKeyToReset() {
        return keyToReset;
    }

    public void setKeyToReset(String keyToReset) {
        this.keyToReset = keyToReset;
    }

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public UserTransaction getUtx() {
        return utx;
    }

    public void setUtx(UserTransaction utx) {
        this.utx = utx;
    }

    public UsuacontJpaController getUsuacontJpaController() {
        return usuacontJpaController;
    }

    public void setUsuacontJpaController(UsuacontJpaController usuacontJpaController) {
        this.usuacontJpaController = usuacontJpaController;
    }

}
